
import 'package:bot_toast/bot_toast.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:single/serial/serial_port_helper.dart';
import 'package:single/util/timer_manager.dart';


import 'entity/book_entity.dart';
import 'entity/current_task_entity.dart';
import 'entity/setting_entity.dart';
import 'http/api_url.dart';
import 'page/index_page.dart';
import 'tts/tts_helper.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();
  Hive.registerAdapter(BookEntityAdapter());
  books = await Hive.openBox('books');
  // // books.deleteFromDisk();
  tts = TtsHelper()..initTTS();
  // await LoggerManager().initLogger();
  eventFactory = EventBus();
  await GetStorage.init();
  final box = GetStorage();
  if(box.hasData('setting_entity')){
    settingEntity = SettingEntity.fromJson(box.read("setting_entity"));
    if(settingEntity.apiAddr != null){
      apiUrl = settingEntity.apiAddr!;
    }
    if(settingEntity.faceApiAddr != null){
      faceUrl = settingEntity.faceApiAddr!;
    }
    if(settingEntity.picApiAddr != null){
      picUrl = settingEntity.picApiAddr!;
    }
    if(settingEntity.appKey != null){
      appKey = settingEntity.appKey!;
    }

  }else{
    settingEntity = SettingEntity();
  }
  try{
    hfSerialPortHelper = SerialPortHelper(settingEntity.hfPort!,115200);
  }catch(e){
    print(e);
  }


  // if(websocket!=null && websocket != ""){
  //   socketHelper = SocketHelper(websocket!);
  // }

  // FlutterError.onError = (FlutterErrorDetails details) async {
  //   LoggerManager().write("error", '${details.exception}\r\n${details.stack}');
  // };
  currentTask = CurrentTaskEntity();

  timer = TimerManager();
  timer.setCountDownTime(fastCountdown: settingEntity.fastCountdown,
      normalCountdown: settingEntity.normalCountdown, slowCountdown: settingEntity.slowCountdown);
  runApp(const MyApp());

}


late EventBus eventFactory;
late SerialPortHelper hfSerialPortHelper;
SerialPortHelper? idSerialPortHelper;
// late SocketHelper socketHelper;
late SettingEntity settingEntity;
late TimerManager timer;
late Box books;
late TtsHelper tts;
late CurrentTaskEntity currentTask;


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
    return  ScreenUtilInit(
      designSize: const Size(400, 640),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (_ , child) {
        return  GetMaterialApp(
            debugShowCheckedModeBanner: false,
            // theme: ThemeData(
            //   fontFamily: 'simsun',
            // ),
            builder: BotToastInit(),
            home: const Index()
        );
      },
    );
  }
}
